package com.show.business.device.service.impl;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.show.business.common.core.domain.AjaxResult;
import com.show.business.device.bean.DeviceGroup;
import com.show.business.device.bean.DeviceGroupAlarmPhone;
import com.show.business.device.bean.DeviceManage;
import com.show.business.device.mapper.DeviceGroupAlarmPhoneMapper;
import com.show.business.device.mapper.DeviceGroupMapper;
import com.show.business.device.mapper.DeviceManageMapper;
import com.show.business.device.service.IDeviceGroupService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

import static com.show.business.common.utils.SecurityUtils.getUsername;

/**
 * <p>
 * 设备分组 服务实现类
 * </p>
 *
 * @author dvlp
 * @since 2024-09-30
 */
@Service
public class DeviceGroupServiceImpl extends ServiceImpl<DeviceGroupMapper, DeviceGroup> implements IDeviceGroupService {
    @Resource
    private DeviceGroupMapper deviceGroupMapper;
    @Resource
    private DeviceGroupAlarmPhoneMapper deviceGroupAlarmPhoneMapper;
    @Resource
    private DeviceManageMapper deviceManageMapper;

    @Override
    public List<DeviceGroup> getList(DeviceGroup deviceGroup) {
        return baseMapper.getList(deviceGroup);
    }

    @Override
    public AjaxResult saveInfo(DeviceGroup deviceGroup) {
        if(StrUtil.isBlank(deviceGroup.getGroupCode())){
            return AjaxResult.error("分组编码不能为空");
        }
        if(StrUtil.isBlank(deviceGroup.getGroupName())){
            return AjaxResult.error("分组名称不能为空");
        }
        if(baseMapper.repeatCodeNum(deviceGroup) > 0){
            return AjaxResult.error("分组编码不能重复");
        }
        if(baseMapper.repeatNameNum(deviceGroup) > 0){
            return AjaxResult.error("分组名称不能重复");
        }
        deviceGroup.setDelFlag("0");
        deviceGroup.setCreateTime(DateTime.now());
        deviceGroup.setCreateBy(getUsername());
        baseMapper.insert(deviceGroup);
        return AjaxResult.success();
    }

    @Override
    public AjaxResult editInfo(DeviceGroup deviceGroup) {
        if(StrUtil.hasBlank(deviceGroup.getGroupCode(),deviceGroup.getGroupCode(),deviceGroup.getId())){
            return AjaxResult.error("必填项不能为空");
        }
        if(baseMapper.repeatCodeNum(deviceGroup) > 0){
            return AjaxResult.error("分组编码不能重复");
        }
        if(baseMapper.repeatNameNum(deviceGroup) > 0){
            return AjaxResult.error("分组名称不能重复");
        }
        deviceGroup.setUpdateTime(DateTime.now());
        deviceGroup.setUpdateBy(getUsername());
        baseMapper.updateById(deviceGroup);
        deviceGroupAlarmPhoneMapper.deleteAlarmPhoneByGroupId(deviceGroup.getId());
        List<DeviceGroupAlarmPhone> alarmPhoneList = deviceGroup.getAlarmPhoneList();
        if(alarmPhoneList != null && alarmPhoneList.size() > 0){
            alarmPhoneList.stream().forEach(k ->{
            k.setGroupId(deviceGroup.getId());
            k.setPhone(k.getPhone());
            deviceGroupAlarmPhoneMapper.insert(k);
            });
        }
        return AjaxResult.success();
    }

    /**
     * 启用设备分组
     * @param groupId
     * @return
     */
    @Override
    public AjaxResult enable(String groupId){
        DeviceGroup group=baseMapper.selectById(groupId);
        group.setGroupStatus("0");
        baseMapper.updateById(group);
        return AjaxResult.success();
    }

    /**
     * 禁用设备分组
     * @param groupId
     * @return
     */
    @Override
    public AjaxResult disable(String groupId){
        DeviceGroup group=baseMapper.selectById(groupId);
        group.setGroupStatus("1");
        baseMapper.updateById(group);
        return AjaxResult.success();
    }

    @Override
    public AjaxResult getInfo(String id) {
        DeviceGroup group=baseMapper.getInfo(id);
        List<DeviceGroupAlarmPhone> phoneList=deviceGroupAlarmPhoneMapper.getListByGroup(group.getId());
        group.setAlarmPhoneList(phoneList);
        List<DeviceManage> deviceList=deviceManageMapper.getListByGroupId(group.getId());
        group.setDeviceManageList(deviceList);
        return AjaxResult.success(group);
    }

    @Override
    public AjaxResult delInfo(String id) {
        List<DeviceManage> deviceList=deviceManageMapper.getListByGroupId(id);
        if(deviceList!=null && deviceList.size()>0){
            return AjaxResult.error("该分组已经分配设备，不能删除");
        }
        baseMapper.updateDel(id);
        return AjaxResult.success();
    }
    @Override
    public AjaxResult delInfos(String[] ids) {
        for(String id:ids){
            List<DeviceManage> deviceList=deviceManageMapper.getListByGroupId(id);
            if(deviceList!=null && deviceList.size()>0){
                return AjaxResult.error("要删除的分组已经分配设备，不能删除");
            }
            baseMapper.updateDel(id);
        }
        return AjaxResult.success();
    }
}
